﻿using Modles.DTO;
using Modles.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static Microsoft.EntityFrameworkCore.DbLoggerCategory;

namespace BilbilDAL
{
    public class ReleaseVideoDAL
    {
        /// <summary>
        /// 根据前端发送的用户信息表id查询并返回视频信息表，按视频发布时间降序排序，提供分页功能
        /// </summary>
        /// <param name="mod"></param>
        /// <param name="TotalCount"></param>
        /// <returns></returns>
        public List<VideoInfo_DTO> GetReleaseVideoByuserId(int UserInfoId , int page, int limit)
        {
            using (VideoPlatformDbContext con = new VideoPlatformDbContext())
            {
                return con.VideoInfos.Where(x => x.UserInfoId == UserInfoId)
                    .OrderByDescending(x => x.Time)
                    .Skip((page - 1) * limit)
                    .Take(limit)
                    .Join(con.Videos, vi => vi.VideoId, v => v.Id, (vi, v) => new { vi, v })
                    .Join(con.Images, temp => temp.vi.ImageId, img => img.Id, (temp, img) => new { temp.vi, temp.v, img })
                    .Join(con.VideoTypes, temp => temp.vi.VideoTypeId, vt => vt.Id, (temp, vt) => new { temp.vi, temp.v, temp.img, vt })
                    .Join(con.UserInfos, temp => temp.vi.UserInfoId, u => u.Id, (temp, u) => new VideoInfo_DTO
                    {
                        Id = temp.vi.Id,
                        VideoId = temp.vi.VideoId,
                        ImageId = temp.vi.ImageId,
                        VideoTypeId = temp.vi.VideoTypeId,
                        UserInfoId = temp.vi.UserInfoId,
                        Title = temp.vi.Title,
                        Briefintroduction = temp.vi.Briefintroduction,
                        Label = temp.vi.Label,
                        Time = temp.vi.Time,
                        Like = temp.vi.Like,
                        Views = temp.vi.Views,
                        VideoAddress = temp.v.Address,
                        ImageAddress = temp.img.Address,
                        VideoType = temp.vt.Type,
                        UserName = u.Name,
                        UserAvatar = u.Avatar
                    })
                    .ToList();
            }
        }
    }
}
